﻿<?php
final class Action 
{
	protected $file;
	protected $class;
	protected $method;
	protected $args = array();

	public function __construct($route, $args = array()) {
		$path = '';
		
		$parts = explode('/', str_replace('../', '', $route));
		
		foreach ($parts as $part) 
		{ 
			$path .= $part;
			
			if (is_dir(APPLICATION_DIR . '/controller/' . $path)) 
			{
				$path .= '/';
				
				array_shift($parts);
				
				continue;
			}

			if (is_file(APPLICATION_DIR . '/controller/' . str_replace('../', '', $path) . '.php')) 
			{
				$this->file = APPLICATION_DIR . '/controller/' . str_replace('../', '', $path) . '.php';
				
				$this->class = 'Controller' . preg_replace('/[^a-zA-Z0-9]/', '', $path);

				array_shift($parts);
				
				break;
			}
			
			if ($args) 
			{
				$this->args = $args;
			}
		}
		
		//echo "<hr>args";
		//var_dump($this->args);
		
		//echo "<hr>class";
		//var_dump($this->class);
		//echo "<hr>";

		$method = array_shift($parts);
		//echo "<hr>method";
		//var_dump($method);
				
		if ($method) 
		{
			$this->method = $method;
		} 
		else 
		{
			$this->method = 'index';
		}
	}
	
	public function getFile() 
	{
		return $this->file;
	}
	
	public function getClass() 
	{
		return $this->class;
	}
	
	public function getMethod() 
	{
		return $this->method;
	}
	
	public function getArgs() 
	{
		return $this->args;
	}
}
?>